package com.ihrm.company.service.impl;

import com.ihrm.common.utils.IdWorker;
import com.ihrm.company.mapper.CompanyMapper;
import com.ihrm.company.service.api.CompanyService;
import com.ihrm.domain.company.Company;
import com.ihrm.domain.company.CompanyExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author chentao
 */
@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * 保存企业
     * 1.配置idwork到工程
     * 2.在service中注入idwork
     * 3.通过idwork生成id
     * 4.保存企业
     */
    @Override
    public void add(Company company) {
        /*得到id并且转化为String类型*/
        String id = idWorker.nextId() + "";
        company.setId(id);
        //默认的状态
        //0 未审核 1 已审核
        company.setAuditState("0");
        //0 未激活 1 已激活
        company.setState(1);
        company.setCreateTime(new Date());
        companyMapper.insertSelective(company);
    }

    /**
     * 更新企业
     * 1.参数：Company
     * 2.根据id查询企业对象
     * 3.设置修改的属性
     * 4.调用mapper完成更新
     */
    @Override
    public void update(Company company) {
        companyMapper.updateByPrimaryKeySelective(company);
    }


    /**
     * 删除企业
     */
    @Override
    public void deleteById(String id) {
        companyMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询企业
     */
    @Override
    public Company findById(String id) {
        return companyMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询企业列表
     */
    @Override
    public List<Company> findAll() {
        return companyMapper.selectByExample(new CompanyExample());
    }
}
